package com.inspur.attendance.Controller;

import com.inspur.attendance.Entity.Overtime;
import com.inspur.attendance.Service.OverTimeService;
import com.inspur.attendance.Service.ServiceImpl.OverTimeServiceImpl;
import com.inspur.attendance.Tools.Return;
import com.inspur.attendance.Tools.TimeConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class OvertimeController {

    @Autowired
    private OverTimeService overTimeService;

    // 获取加班信息
    @RequestMapping("/getAllOverTime")
    public Return getAllOverTime(@Param("pageNo") int pageNo,
                                 @Param("pageSize") int pageSize) throws Exception {
        return Return.success(overTimeService.getAllOverTime(pageNo, pageSize));
    }

    @RequestMapping("/getMyOverTime")
    public Return getMyOverTime(@Param("pageNo") int pageNo,
                                @Param("pageSize") int pageSize,
                                @Param("work_id") String work_id) throws Exception {
        return Return.success(overTimeService.getMyOverTime(pageNo, pageSize, work_id));
    }

    @RequestMapping("/myOvertimeRequest")
    public Return myOvertimeRequest(@RequestParam("id") String id) {
        System.out.println(id);
        return Return.success(overTimeService.MyOvertimeRequest(id));
    }

    @RequestMapping("/overtimeRequest")
    public Return overtimeRequest(@Param("departmentId") String departmentId) {
        return Return.success(overTimeService.overtimeRequest(departmentId));
    }

    // 0为未处理
    // 1为允许
    // 2为打回
    @RequestMapping("/isAgree")
    public Return isAgree(@RequestParam("id") Long id,
                          @RequestParam("isAgree") Integer isAgree,
                          @RequestParam("approver") String approver
                          ) throws Exception {
        Overtime overtime = overTimeService.findById(id);
        overtime.setIsAgree(isAgree);
        overtime.setApproveTime(new Date());
        overtime.setApprover(approver);
        System.out.println(overtime);
        overTimeService.addOverTime(overtime);
        return Return.success();
    }

    // 添加加班申请
    @RequestMapping("/addOverTime")
    public Return addOverTime(@Param("reason") String reason,
                              @Param("startTime") String startTime,
                              @Param("endTime") String endTime,
                              @Param("departmentId") String departmentId,
                              @Param("name") String name,
                              @Param("workId") String workId) throws IllegalAccessException {
        Date startTime1 = TimeConvert.stringToDate(startTime);
        Date endTime1 = TimeConvert.stringToDate(endTime);

        Overtime overtime  = new Overtime();
        overtime.setReason(reason);
        overtime.setStartTime(startTime1);
        overtime.setEndTime(endTime1);
        overtime.setDepartment(departmentId);
        overtime.setIsAgree(0);
        overtime.setCreateTime(new Date());
        overtime.setWorkId(workId);
        overtime.setName(name);
        System.out.println(overtime);
        try {
            overTimeService.addOverTime(overtime);
            return Return.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Return.failure();
        }
    }

    // 添加加班申请
    @RequestMapping("/editOverTime")
    public Return editOverTime(@Param("reason") String reason,
                              @Param("startTime") String startTime,
                              @Param("endTime") String endTime,
                              @Param("departmentId") String departmentId,
                              @Param("name") String name,
                              @Param("workId") String workId,
                               @Param("id") Long id) throws Exception {
        Date startTime1 = TimeConvert.stringToDate(startTime);
        Date endTime1 = TimeConvert.stringToDate(endTime);

        Overtime overtime = overTimeService.findById(id);
        overtime.setReason(reason);
        overtime.setStartTime(startTime1);
        overtime.setEndTime(endTime1);
        overtime.setDepartment(departmentId);
        overtime.setIsAgree(0);
        overtime.setCreateTime(new Date());
        overtime.setWorkId(workId);
        overtime.setName(name);
        System.out.println(overtime);
        try {
            overTimeService.addOverTime(overtime);
            return Return.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Return.failure();
        }
    }

    // 删除加班申请
    @RequestMapping("/delOverTime")
    public Return delOverTime(@RequestParam("id") Long id) {
        try {
            overTimeService.delOverTime(id);
            return Return.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Return.failure();
        }
    }
}
